{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "comparable-introduction",
   "metadata": {},
   "source": [
    "# Norwegian Bank Account Numbers"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "narrative-supervisor",
   "metadata": {},
   "source": [
    "## Introduction"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "married-trustee",
   "metadata": {},
   "source": [
    "The function `clean_no_kontonr()` cleans a column containing Norwegian bank account number (kontonr) strings, and standardizes them in a given format. The function `validate_no_kontonr()` validates either a single kontonr strings, a column of kontonr strings or a DataFrame of kontonr strings, returning `True` if the value is valid, and `False` otherwise."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "reported-keeping",
   "metadata": {},
   "source": [
    "kontonr strings can be converted to the following formats via the `output_format` parameter:\n",
    "\n",
    "* `compact`: only number strings without any seperators or whitespace, like \"86011117947\"\n",
    "* `standard`: kontonr strings with proper whitespace in the proper places, like \"8601.11.17947\"\n",
    "* `iban`: convert the number to an IBAN, like \"NO93 8601 11 17947\".\n",
    "\n",
    "Invalid parsing is handled with the `errors` parameter:\n",
    "\n",
    "* `coerce` (default): invalid parsing will be set to NaN\n",
    "* `ignore`: invalid parsing will return the input\n",
    "* `raise`: invalid parsing will raise an exception\n",
    "\n",
    "The following sections demonstrate the functionality of `clean_no_kontonr()` and `validate_no_kontonr()`. "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "administrative-yugoslavia",
   "metadata": {},
   "source": [
    "### An example dataset containing kontonr strings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ancient-success",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "df = pd.DataFrame(\n",
    "    {\n",
    "        \"kontonr\": [\n",
    "            \"8601 11 17947\",\n",
    "            \"8601 11 17949\",\n",
    "            \"999 999 999\",\n",
    "            \"004085616\",\n",
    "            \"002 724 334\",\n",
    "            \"hello\",\n",
    "            np.nan,\n",
    "            \"NULL\",\n",
    "        ], \n",
    "        \"address\": [\n",
    "            \"123 Pine Ave.\",\n",
    "            \"main st\",\n",
    "            \"1234 west main heights 57033\",\n",
    "            \"apt 1 789 s maple rd manhattan\",\n",
    "            \"robie house, 789 north main street\",\n",
    "            \"1111 S Figueroa St, Los Angeles, CA 90015\",\n",
    "            \"(staples center) 1111 S Figueroa St, Los Angeles\",\n",
    "            \"hello\",\n",
    "        ]\n",
    "    }\n",
    ")\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "academic-cisco",
   "metadata": {},
   "source": [
    "## 1. Default `clean_no_kontonr`\n",
    "\n",
    "By default, `clean_no_kontonr` will clean kontonr strings and output them in the standard format with proper separators."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "romance-macintosh",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dataprep.clean import clean_no_kontonr\n",
    "clean_no_kontonr(df, column = \"kontonr\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "exclusive-flour",
   "metadata": {},
   "source": [
    "## 2. Output formats"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "novel-adventure",
   "metadata": {},
   "source": [
    "This section demonstrates the output parameter."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bulgarian-neutral",
   "metadata": {},
   "source": [
    "### `standard` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "systematic-leeds",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_no_kontonr(df, column = \"kontonr\", output_format=\"standard\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "interested-forth",
   "metadata": {},
   "source": [
    "### `compact`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "seven-bathroom",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_no_kontonr(df, column = \"kontonr\", output_format=\"compact\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "correct-taiwan",
   "metadata": {},
   "source": [
    "### `iban`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "covered-assembly",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_no_kontonr(df, column = \"kontonr\", output_format=\"iban\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "olympic-floor",
   "metadata": {},
   "source": [
    "## 3. `inplace` parameter\n",
    "\n",
    "This deletes the given column from the returned DataFrame. \n",
    "A new column containing cleaned kontonr strings is added with a title in the format `\"{original title}_clean\"`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "standing-projector",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_no_kontonr(df, column=\"kontonr\", inplace=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "third-archive",
   "metadata": {},
   "source": [
    "## 4. `errors` parameter"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "subjective-ghost",
   "metadata": {},
   "source": [
    "### `coerce` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fatal-adventure",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_no_kontonr(df, \"kontonr\", errors=\"coerce\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "talented-airline",
   "metadata": {},
   "source": [
    "### `ignore`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fifty-radio",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_no_kontonr(df, \"kontonr\", errors=\"ignore\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "moderate-shape",
   "metadata": {},
   "source": [
    "## 4. `validate_no_kontonr()`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "nasty-protocol",
   "metadata": {},
   "source": [
    "`validate_no_kontonr()` returns `True` when the input is a valid kontonr. Otherwise it returns `False`.\n",
    "\n",
    "The input of `validate_no_kontonr()` can be a string, a Pandas DataSeries, a Dask DataSeries, a Pandas DataFrame and a dask DataFrame.\n",
    "\n",
    "When the input is a string, a Pandas DataSeries or a Dask DataSeries, user doesn't need to specify a column name to be validated. \n",
    "\n",
    "When the input is a Pandas DataFrame or a dask DataFrame, user can both specify or not specify a column name to be validated. If user specify the column name, `validate_no_kontonr()` only returns the validation result for the specified column. If user doesn't specify the column name, `validate_no_kontonr()` returns the validation result for the whole DataFrame."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "palestinian-seeking",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dataprep.clean import validate_no_kontonr\n",
    "print(validate_no_kontonr(\"8601 11 17947\"))\n",
    "print(validate_no_kontonr(\"8601 11 17949\"))\n",
    "print(validate_no_kontonr(\"999 999 999\"))\n",
    "print(validate_no_kontonr(\"51824753556\"))\n",
    "print(validate_no_kontonr(\"004085616\"))\n",
    "print(validate_no_kontonr(\"hello\"))\n",
    "print(validate_no_kontonr(np.nan))\n",
    "print(validate_no_kontonr(\"NULL\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "foster-automation",
   "metadata": {},
   "source": [
    "### Series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "automatic-train",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_no_kontonr(df[\"kontonr\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "composed-september",
   "metadata": {},
   "source": [
    "### DataFrame + Specify Column"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "northern-glory",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_no_kontonr(df, column=\"kontonr\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "certified-rabbit",
   "metadata": {},
   "source": [
    "### Only DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "superior-model",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_no_kontonr(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "charged-nylon",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
